﻿using System;
using System.Windows;
using System.Windows.Controls;
using JiveMessenger.User;

namespace JiveMessenger.Views
{
    public enum OptionsPage
    {
        Personal = 0,
        Layout = 1,
        SignIn = 2,
        Sounds = 3,
        Message = 4,
        Misc = 5,
        ImagePreview = 6
    }
    /// <summary>
    /// Interaction logic for Options.xaml
    /// </summary>
    public partial class JmOptions : Window
    {
        private IClient _currentClient { get; set; }
        PersonalPage Personal { get; set; }
        LayoutPage Layout { get; set; }
        SignInPage Signin { get; set; }
        SoundsPage Sounds { get; set; }
        MessagesPage Message { get; set; }
        MiscPage Misc { get; set; }
        ImagePreviewPage ImagePreview { get; set; }
        public JmOptions(IClient currentClient, OptionsPage page = OptionsPage.Personal)
        {
            InitializeComponent();
            _currentClient = currentClient;
            this.Personal = new PersonalPage(currentClient);
            this.Message = new MessagesPage(currentClient);

            this.Menu.SelectedItem = this.Menu.Items[(int)page];
            this.Menu.Focus();

        }
        private void NavigateToPage(OptionsPage page)
        {
            switch (page)
            {
                case OptionsPage.Personal:
                    if (this.Personal == null)
                        this.Personal = new PersonalPage(_currentClient);
                    OptionsFrame.Navigate(Personal);
                    break;
                case OptionsPage.Message:
                    if (this.Message == null)
                        this.Message = new MessagesPage(_currentClient);
                    OptionsFrame.Navigate(Message);
                    break;
                case OptionsPage.Layout:
                    if (this.Layout == null)
                        this.Layout = new LayoutPage();
                    OptionsFrame.Navigate(Layout);
                    break;
                case OptionsPage.Misc:
                    if (this.Misc == null)
                        this.Misc = new MiscPage();
                    OptionsFrame.Navigate(Misc);
                    break;
                case OptionsPage.SignIn:
                    if (this.Signin == null)
                        this.Signin = new SignInPage();
                    OptionsFrame.Navigate(Signin);
                    break;
                case OptionsPage.Sounds:
                    if (this.Sounds == null)
                        this.Sounds = new SoundsPage();
                    OptionsFrame.Navigate(Sounds);
                    break;
                case OptionsPage.ImagePreview:
                    if (this.ImagePreview == null)
                        this.ImagePreview = new ImagePreviewPage();
                    OptionsFrame.Navigate(ImagePreview);
                    break;

            }
        }
        private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lv = (ListBox)sender;
            ListBoxItem lvi = lv.SelectedItem as ListBoxItem;
            if (lvi != null)
                switch (lvi.Name)
                {
                    case "OptionMenuPersonal":
                        NavigateToPage(OptionsPage.Personal);
                        break;
                    case "OptionMenuLayout":
                        NavigateToPage(OptionsPage.Layout);
                        break;
                    case "OptionMenuSignIn":
                        NavigateToPage(OptionsPage.SignIn);
                        break;
                    case "OptionMenuMessages":
                        NavigateToPage(OptionsPage.Message);
                        break;
                    case "OptionMenuSounds":
                        NavigateToPage(OptionsPage.Sounds);
                        break;
                    case "OptionMenuMisc":
                        NavigateToPage(OptionsPage.Misc);
                        break;
                    case "OptionMenuImagePreview":
                        NavigateToPage(OptionsPage.ImagePreview);
                        break;
                }
        }

        private void SavePersonalPage()
        {
            _currentClient.UserSettings.NickName = this.Personal.txtBoxNickname.Text;
            string hash = "";
            string extension = "";
            if (this.Personal.AvatarFile != null)
                extension = this.Personal.AvatarFile.Extension;
            if (this.Personal.AvatarImageSource != null)
            {
                hash = this.Personal.AvatarImageSource.GetMD5();
                _currentClient.SaveAvatarToDisk(_currentClient.CurrentUserInfo, this.Personal.AvatarImageSource, hash, extension);
            }


            _currentClient.UserSettings.Avatar = new JmShared.Avatar { Hash = hash, Extension = extension };



        }
        private void SaveLayoutPage()
        {
            if (this.Layout == null)
                return;

            Properties.Settings.Default.SettingEnableMainMenu = this.Layout.chkBoxEnableMainMenu.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingFlashWindowOnNewMessage = this.Layout.chkBoxEnableWindowFlashing.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingAnimateEmoticonListEmoticons = this.Layout.chkBoxEnableEmoticonListAnimations.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingPrivateMessageLocation = this.Layout.PrivateMessageBehavior.ToString();
        }

        private void SaveImagePreviewPage()
        {
            if (this.ImagePreview == null)
                return;
            Properties.Settings.Default.SettingEnableHoverImage = this.ImagePreview.chkBoxEnableHoverImage.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingEnableGfyCat = this.ImagePreview.chkBoxEnableGfyCat.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingGfyCatVideoFormat = this.ImagePreview.GfyCatVideoFormat.ToString();
            Properties.Settings.Default.SettingEnableImgurVideo = this.ImagePreview.chkBoxEnableImgurVideo.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingImgurVideoFormat = this.ImagePreview.ImgurVideoFormat.ToString();
        }

        private void SaveMessagePage()
        {
            if (this.Message == null)
                return;
            _currentClient.UserSettings.EnableNudge = this.Message.ChkBoxNudge.IsChecked.GetValueOrDefault();
            _currentClient.UserSettings.GroupMessages = this.Message.ChkBoxGroupMsg.IsChecked.GetValueOrDefault();
            _currentClient.UserSettings.ShowTimestamp = this.Message.ChkBoxShowTimestamps.IsChecked.GetValueOrDefault();
            _currentClient.UserSettings.EnableChatLog = this.Message.ChkBoxEnableChatLog.IsChecked.GetValueOrDefault();
            _currentClient.UserSettings.StoreMessagesWhileOffline = this.Message.ChkBoxRetreiveOfflineMessages.IsChecked.GetValueOrDefault();
            _currentClient.UserSettings.ReceiveAnsweredMessagesWhileOffline = this.Message.ChkBoxRetreiveAnsweredMessages.IsChecked.GetValueOrDefault();
            _currentClient.UserSettings.PauseEmoticonsOnFocusLoss = this.Message.ChkBoxPauseEmoticonsOnFocusLoss.IsChecked.GetValueOrDefault();
            _currentClient.UserSettings.EnableWinampNowPlaying = this.Message.CheckBoxEnableWinampNowPlaying.IsChecked.GetValueOrDefault();
            _currentClient.UserSettings.EnableSpotifyNowPlaying = this.Message.CheckBoxEnableSpotifyNowPlaying.IsChecked.GetValueOrDefault();
            _currentClient.UserSettings.WinampKeyGesture = this.Message.TextBoxWinampShortcut.Text;
            _currentClient.UserSettings.SpotifyKeyGesture = this.Message.TextBoxSpotifyShortcut.Text;
            _currentClient.UserSettings.NowPlayingFormat = this.Message.TxtBoxNowPlayingFormat.Text;
            _currentClient.UserSettings.AnimateGifEmoticons = this.Message.ChkBoxAnimateGifEmoticons.IsChecked.GetValueOrDefault();

            Int32 age;
            if (Int32.TryParse(this.Message.TxtBoxOfflineMessageAgeLimit.Text, out age))
            {
                if (age <= 48)
                    _currentClient.UserSettings.OfflineMessagesAgeLimit = age;
            }

            Int32 count;
            if (Int32.TryParse(this.Message.TxtBoxOfflineMessageCount.Text, out count))
            {
                if (count <= 250)
                    _currentClient.UserSettings.OfflineMessagesCountLimit = count;
            }
        }

        private void SaveSoundsPage()
        {
            if (this.Sounds == null)
                return;
            Properties.Settings.Default.SettingEnableGroupMessageNotificationSound = Sounds.chkBoxEnableGroupMessageSound.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingEnableNudgeNotificationSound = Sounds.chkBoxEnableNudgeSound.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingEnablePrivateMessageNotificationSound = Sounds.chkBoxEnablePrivateMessageSound.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingEnableSounds = Sounds.chkBoxEnableSounds.IsChecked.GetValueOrDefault();
        }

        private void SaveSigninPage()
        {
            if (this.Signin == null)
                return;
            Properties.Settings.Default.SettingStartWithWindows = Signin.chkBoxStartupWithWindows.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.LoginAutomatically = Signin.chkBoxAutomaticSignin.IsChecked.GetValueOrDefault();
            Properties.Settings.Default.SettingStartMinimized = Signin.chkBoxStartMinimized.IsChecked.GetValueOrDefault();
        }

        private void SaveMiscPage()
        {
            if (this.Misc == null)
                return;
            Properties.Settings.Default.SettingDebugLogLevel = this.Misc.ComboBoxDebugLevel.Text;
            Properties.Settings.Default.SettingEnableDebugLogging = this.Misc.ChkDebugLoggingEnabled.IsChecked.GetValueOrDefault();
        }

        private void BnOK_Click(object sender, RoutedEventArgs e)
        {
            SavePersonalPage();
            SaveLayoutPage();
            SaveSoundsPage();
            SaveMessagePage();
            SaveMiscPage();
            SaveSigninPage();
            SaveImagePreviewPage();
            this.DialogResult = true;
        }

        private void BnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }


}
